package com.optitronics.beans;

import java.io.IOException;
import java.math.BigDecimal;

import org.springframework.web.multipart.MultipartFile;

import com.optitronics.util.OptitronicsConstants;

public class ShoppingCartItem {

	private Long itemId;

	private Integer orderType;

	private Product product;

	private Integer productId;

	private String productName;

	private BigDecimal basicPrice;

	private BigDecimal discount;

	private BigDecimal priceWithDiscount;

	private BigDecimal amountWithoutTax;

	private BigDecimal amountWithTax;

	private Integer quantity;

	private String buyerName;

	private String buyerEmail;

	private String buyerAddress;

	private String buyerCity;

	private String buyerContactPerson;

	private String buyerPhone;

	private MultipartFile file;

	private byte[] fileContent;

	public ShoppingCartItem() {
		this.itemId = System.currentTimeMillis();
	}

	public ShoppingCartItem(Long itemId) {
		this.itemId = itemId;
	}

	public void loadFileContent() {

		if (OptitronicsConstants.ORDER_TYPE_RENEWAL.equals(orderType)) {
			try {
				byte[] uploadedFileContent = file.getBytes();
				this.fileContent = new byte[uploadedFileContent.length];
				for (int i = 0; i < uploadedFileContent.length; i++) {
					this.fileContent[i] = uploadedFileContent[i];
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public ShoppingCartItem(Product product) {
		this.itemId = System.currentTimeMillis();
		this.productId = product.getId();
		this.productName = product.getName();
		this.basicPrice = product.getPriceNew();
		this.discount = product.getDiscount();
		BigDecimal discount = product.getDiscount() != null ? product.getDiscount() : BigDecimal.ZERO;
		this.priceWithDiscount = BigDecimal.valueOf(100.00).subtract(discount).divide(BigDecimal.valueOf(100.00))
				.multiply(this.basicPrice);
		this.priceWithDiscount = this.priceWithDiscount.setScale(2, BigDecimal.ROUND_HALF_UP);

		this.product = product;
	}

	@Override
	public boolean equals(Object obj) {
		return ((ShoppingCartItem) obj).itemId.equals(this.itemId);
	}

	public Integer getProductId() {
		return productId;
	}

	public void setProductId(Integer productId) {
		this.productId = productId;
	}

	public BigDecimal getAmountWithoutTax() {
		return amountWithoutTax;
	}

	public void setAmountWithoutTax(BigDecimal amountWithoutTax) {
		this.amountWithoutTax = amountWithoutTax;
	}

	public BigDecimal getAmountWithTax() {
		return amountWithTax;
	}

	public void setAmountWithTax(BigDecimal amountWithTax) {
		this.amountWithTax = amountWithTax;
	}

	public Integer getQuantity() {
		return quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	public String getBuyerName() {
		return buyerName;
	}

	public void setBuyerName(String buyerName) {
		this.buyerName = buyerName;
	}

	public String getBuyerEmail() {
		return buyerEmail;
	}

	public void setBuyerEmail(String buyerEmail) {
		this.buyerEmail = buyerEmail;
	}

	public String getBuyerAddress() {
		return buyerAddress;
	}

	public void setBuyerAddress(String buyerAddress) {
		this.buyerAddress = buyerAddress;
	}

	public String getBuyerCity() {
		return buyerCity;
	}

	public void setBuyerCity(String buyerCity) {
		this.buyerCity = buyerCity;
	}

	public String getBuyerContactPerson() {
		return buyerContactPerson;
	}

	public void setBuyerContactPerson(String buyerContactPerson) {
		this.buyerContactPerson = buyerContactPerson;
	}

	public String getBuyerPhone() {
		return buyerPhone;
	}

	public void setBuyerPhone(String buyerPhone) {
		this.buyerPhone = buyerPhone;
	}

	public Long getItemId() {
		return itemId;
	}

	public void setItemId(Long itemId) {
		this.itemId = itemId;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Integer getOrderType() {
		return orderType;
	}

	public void setOrderType(Integer orderType) {
		this.orderType = orderType;
	}

	public BigDecimal getBasicPrice() {
		return basicPrice;
	}

	public void setBasicPrice(BigDecimal basicPrice) {
		this.basicPrice = basicPrice;
	}

	public BigDecimal getPriceWithDiscount() {
		return priceWithDiscount;
	}

	public void setPriceWithDiscount(BigDecimal priceWithDiscount) {
		this.priceWithDiscount = priceWithDiscount;
	}

	public BigDecimal getDiscount() {
		return discount;
	}

	public void setDiscount(BigDecimal discount) {
		this.discount = discount;
	}

	public MultipartFile getFile() {
		return file;
	}

	public void setFile(MultipartFile file) {
		this.file = file;
	}

	public byte[] getFileContent() {
		return fileContent;
	}

	public void setFileContent(byte[] fileContent) {
		this.fileContent = fileContent;
	}

}
